package com.contactbook.web.client;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class DeleteAccountWidget extends View {
	
	private class OkHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent arg0) {
			deleteAccount();			
		}		
	}
	
	private class CancelHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent arg0) {
			contactBook.switchView(Constants.MyAccountViewName);			
		}		
	}
	
	private VerticalPanel content;
	
	private static final String TITLE = "Delete your account";
	
	public DeleteAccountWidget(ContactBook contactBook) {
		super(contactBook);
		content = new VerticalPanel();
	}
	
	@Override
	public VerticalPanel getContent() {
		if (contactBook.isLogged()) {
			if (content == null)
				return buildView();
			return content;
		} else
			contactBook.switchView("login");
		return null;
	}

	private VerticalPanel buildView() {
		DecoratorPanel panelDelete = new DecoratorPanel();
		VerticalPanel panelWidgets = new VerticalPanel();
		
		HorizontalPanel panelLabelConfirm = new HorizontalPanel();
		panelLabelConfirm.setWidth("290px");
		panelLabelConfirm.setHeight("90px");
		
		Label labelConfirm = new Label();
		labelConfirm.setStyleName("gwt-label-labelnote");
		
		String message = "You are going to delete your account.'";		
		message += "'You are going to erase all your contacts from your contact book. Do you confirm that you want to delete your contact book ?";
		labelConfirm.setText(message);
		
		panelLabelConfirm.add(labelConfirm);
		
		panelWidgets.add(panelLabelConfirm);
		
		FlexTable tableButtons = new FlexTable();
		tableButtons.setWidth("290px");
		
		Button okButton = new Button("Ok");
		okButton.setStyleName("buttonform");
		okButton.addClickHandler(new OkHandler());
		
		Button cancelButton = new Button("Cancel");
		cancelButton.setStyleName("buttonform");
		cancelButton.addClickHandler(new CancelHandler());
		
		tableButtons.setWidget(0, 0, okButton);
		tableButtons.setWidget(0, 1, cancelButton);
		
		/* Table buttons properties */
		tableButtons.getFlexCellFormatter().setHeight(0, 0, "60px");
		
		tableButtons.getFlexCellFormatter().setAlignment(0, 0, ALIGN_CENTER, ALIGN_MIDDLE);
		tableButtons.getFlexCellFormatter().setAlignment(0, 1, ALIGN_CENTER, ALIGN_MIDDLE);
		
		tableButtons.setCellPadding(8);
		
		panelWidgets.add(tableButtons);
		panelWidgets.setSpacing(10);
		panelDelete.add(panelWidgets);
		
		content.add(panelDelete);
		
		return content;
	}

	@Override
	public String getTitle() {
		return TITLE;
	}
	
	private void deleteAccount() {
		AsyncCallback<Void> callback = new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable arg0) {				
			}

			@Override
			public void onSuccess(Void arg0) {
				contactBook.switchView(Constants.LoginViewName);
				
			}			
		};
		contactBook.getAccountService().delete(callback);
	}
	
	void destroyCurrentSession() {
		contactBook.getCurrentSession().parameters.clear();
		contactBook.getCurrentSession().logout();
	}
}
